package com.bawei.newsapplication.view.activity.channel

import android.content.Intent
import android.widget.Toast
import coil.load
import com.bawei.coom.model.myapp.MyAPP
import com.bawei.newsapplication.R
import com.bawei.newsapplication.base.BaseActivity
import com.bawei.newsapplication.databinding.ActivityQuestionBinding
import com.bawei.newsapplication.viewmodel.MessageViewModel
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * 作者: 谢庆平
 * 时间: 2024/10/31 14:22
 * 描述:发布提问页面
 */
class QuestionActivity : BaseActivity<MessageViewModel,ActivityQuestionBinding>(R.layout.activity_question) {
  var file: File= File("")
    override fun initView() {
        mDataBinding.questionSubmit.setOnClickListener {
//            val load = mDataBinding.questionImage.load("https://www.baidu.com/img/bd_logo1.png")
//            val questionEdit = mDataBinding.questionEdit.text.toString()
           val userId:String= MyAPP.userId
           val token:String= MyAPP.token
            val content:String= mDataBinding.questionEdit.text.toString()
            if (content.isEmpty()) {
                Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
                val media:List<MultipartBody.Part>
                val filePath = "path/to/your/image.jpg"
                val file = File(filePath)
                val requestBody = file.asRequestBody("image/jpeg".toMediaType())
                val part = MultipartBody.Part.createFormData("image", file.name, requestBody)
                media = listOf(part)
            mViewModel.getQuestion(userId,token,content, media.toString())
        }
        mDataBinding.questionImage.setOnClickListener {
            startActivityForResult(Intent(Intent.ACTION_PICK).setType("image/*"), 1)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            val uri = data?.data
            mDataBinding.questionImage.load(uri)
        }
    }
    override fun dataObserve() {
        mViewModel.questionData.observe(this) {
            Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
        }
    }

}